package com.tony.mindhr.controller.system.basic;

import com.tony.mindhr.model.Menu;
import com.tony.mindhr.model.RespBean;
import com.tony.mindhr.model.Role;
import com.tony.mindhr.service.MenuService;
import com.tony.mindhr.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: PermissionController
 * @Auther: 皮蛋布丁
 * @Date: 2021/06/01/22:46
 * @Description:权限组用户角色
 */
@RestController
@RequestMapping("/system/basic/permiss")
public class PermissionController {

    @Autowired
    RoleService roleService;
    @Autowired
    MenuService menuService;
    /**
    * @Description: getAllroles 获取所有角色
    * @Param: []
    * @return: java.util.List<com.tony.mindhr.model.Role>
    * @Author: 皮蛋布丁
    * @Date: 2021/6/1 22:55
    */
    @GetMapping("/")
    public List<Role> getAllroles() {
        return roleService.getAllroles();
    }
    
    /**
    * @Description: getAllMenus 获取菜单树
    * @Param: []
    * @return: java.util.List<com.tony.mindhr.model.Menu>
    * @Author: 皮蛋布丁
    * @Date: 2021/6/3 20:24
    */
    @GetMapping("/menu")
    public List<Menu> getAllMenus() {
        return menuService.getAllMenus();
    }

    /**
    * @Description: getMidsByRid 根据用户id获取菜单id
    * @Param: [rid]
    * @return: java.util.List<java.lang.Integer>
    * @Author: 皮蛋布丁
    * @Date: 2021/6/3 20:26
    */
    @GetMapping("/mid/{rid}")
    public List<Integer> getMidsByRid(@PathVariable Integer rid) {
        return menuService.getMidsByRid(rid);
    }

    /**
    * @Description: updateMenuRole 根据用户id修改菜单(先将原有的删除，再插入新的)
    * @Param: [rid, mids]
    * @return: com.tony.mindhr.model.RespBean
    * @Author: 皮蛋布丁
    * @Date: 2021/6/3 21:02
    */
    @PutMapping("/")
    public RespBean updateMenuRole(Integer rid,Integer[] mids) {
        if (menuService.updateMenuRole(rid,mids)) {
            return RespBean.ok("更新成功！");
        }
        return RespBean.error("更新失败！");
    }

    /**
    * @Description: addRole 添加角色
    * @Param: [role]
    * @return: com.tony.mindhr.model.RespBean
    * @Author: 皮蛋布丁
    * @Date: 2021/6/4 22:20
    */
    @PostMapping("/role")
    public RespBean addRole(@RequestBody Role role) {
        if (role.getName().trim().isEmpty() && role.getNameZh().trim().isEmpty()) {
            return RespBean.error("角色英文名和中文名都为空字符串！");
        }
        else if (role.getName().equalsIgnoreCase("") || role.getName().trim().isEmpty()) {
            return RespBean.error("角色英文名为空字符串！");
        }
        else if (role.getNameZh().equalsIgnoreCase("") || role.getNameZh().trim().isEmpty()) {
            return RespBean.error("角色中文名为空字符串！");
        }
        if (roleService.addRole(role) == 1) {
            return RespBean.ok("添加成功！");
        }
        return RespBean.error("添加失败！");
    }


    /**
    * @Description: deleteRoleById 根据角色id删除规则
    * @Param: [rid]
    * @return: com.tony.mindhr.model.RespBean
    * @Author: 皮蛋布丁
    * @Date: 2021/6/5 15:51
    */
    @DeleteMapping("/role/{rid}")
    public RespBean deleteRoleById(@PathVariable Integer rid) {
        if (roleService.deleteRoleById(rid) == 1) {
            return RespBean.ok("删除成功！");
        }
        return RespBean.error("删除失败！");
    }








}
